package main

import (
    "fmt"
    "math"
)

func main() {
    // example 1.1
    var v1 int
    var v2 int
    v1 = 100
    fmt.Println("Values stored in variable v1 ::", v1)
    fmt.Println("Values stored in variable v2 ::", v2)

    // example 1.2
    // := operator allows short declaration of variable
    v3 := 100
    fmt.Println("Values stored in variable v3 ::", v3)

    // example 1.3
    // Byte are alias of uint8
    // Rune are alias of int32 and are used to store Unicode characters
    maxInt8 := math.MaxInt8
    minInt8 := math.MinInt8
    fmt.Println("Range of Int8 ::", minInt8, "to", maxInt8)

    maxInt16 := math.MaxInt16
    minInt16 := math.MinInt16
    fmt.Println("Range of Int16 ::", minInt16, "to", maxInt16)

    maxInt32 := math.MaxInt32
    minInt32 := math.MinInt32
    fmt.Println("Range of Int32 ::", minInt32, "to", maxInt32)

    maxInt64 := math.MaxInt64
    minInt64 := math.MinInt64
    fmt.Println("Range of Int64 ::", minInt64, "to", maxInt64)

    maxUint8 := math.MaxUint8
    maxUint16 := math.MaxUint16
    maxUint32 := math.MaxUint32
    // 直接使用 := 会报错
    var maxUint64 uint64
    maxUint64 = math.MaxUint64
    fmt.Println("Max Uint8 ::", maxUint8)
    fmt.Println("Max Uint16 ::", maxUint16)
    fmt.Println("Max Uint32 ::", maxUint32)
    fmt.Println("Max Uint64 ::", maxUint64)

    maxFloat32 := math.MaxFloat32
    maxFloat64 := math.MaxFloat64
    fmt.Println("Max Float32 ::", maxFloat32)
    fmt.Println("Max Float64 ::", maxFloat64)

    // string
    s := "hello, World!"
    r := []rune(s)
    r[0] = 'H'
    s2 := string(r)
    fmt.Println(r)
    fmt.Println(s2)

    mystring := "Hello World!"
    // used to find the number of character in mystring
    length := len(mystring)
    fmt.Println(length)
    // two strings are concatenated into a single string
    fmt.Println("hello" + "world")
    // equality can be tested using "==" sign
    fmt.Println("world" == "hello")
    fmt.Println("world" == "world")
    // Unicode value can also be used to compare
    fmt.Println("a" < "b")
    fmt.Println("b" < "a")
    // Indexing: String are indexed same as array.
    fmt.Println(string(mystring[0]))
    // Slicing
    fmt.Println(mystring[1:4])

    x := 10
    y := 12
    fmt.Println(more(x, y))
    fmt.Println(max(x, y))
    fmt.Println(maxAreaCheck(x, y, 100))

    //example 1.8 switch
    i := 2
    switch i {
    case 1:
        fmt.Println("one")
    case 2:
        fmt.Println("two")
    case 3:
        fmt.Println("three")
    default:
        fmt.Println("something else")
    }

    //example 1.9
    switch i {
    case 1, 2, 3:
        fmt.Println("one, two or three")
    default:
        fmt.Println("something else")
    }
    // example 1.10
    // 66是偶数吗？
    isEven(66) // true

    // example 1.11
    numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    sum := 0
    for i := 0; i < len(numbers); i++ {
        sum += numbers[i]
    }
    fmt.Println("Sum is ::", sum)

    // example 1.12
    numbers2 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    sum2 := 0
    i2 := 0
    n := len(numbers2)
    for i2 < n {
        sum2 += numbers2[i2]
        i2++
    }
    fmt.Println("Sum is ::", sum2)

    // example 1.13
    numbers3 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    sum3 := 0
    i3 := 0
    n3 := len(numbers3)
    for {
        sum3 += numbers3[i3]
        i3++
        if i3 >= n3 {
            break
        }
    }
    fmt.Println("Sum is :: ", sum3)
}

// example 1.5
func more(x, y int) bool {
    //basic if
    if x > y {
        return true
    }
    return false
}

// example 1.6
func max(x, y int) int {
    var max int
    if x > y {
        max = x
    } else {
        max = y
    }
    return max
}

//  if with precondition
func maxAreaCheck(length, width, limit int) bool {
    if area := length * width; area < limit {
        return true
    } else {
        return false
    }
}

//example 1.10
func isEven(value int) {
    switch {
    case value%2 == 0:
        fmt.Println("I is even")
    default:
        fmt.Println("I is odd")
    }
}
